#include "mainscene.h"
#include "ui_mainscene.h"


MainScene::MainScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainScene)
{
    ui->setupUi(this);

    // 配置主场景
    this->setFixedSize(350, 588);
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));
    this->setWindowTitle("Coin Flip");

    // 退出场景
    connect(ui->actionquit, &QAction::triggered, [=]() {
                this->close();
    });


    // 按钮音效资源
    QSound *startSound = new QSound(":/res/TapButtonSound.wav", this);
    // 设置音效循环
    // startSound->setLoops(-1); // 无限循环

    MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5 - startBtn->width()*0.5, this->height()*0.7);

    // 实例化选择关卡场景
    chooseScene = new ChooseLevelScene;

    connect(startBtn, &MyPushButton::clicked, [=]() {

        startSound->play();
        startBtn->zoom1();
        startBtn->zoom2();

        // 延时进入选择关卡场景

        QTimer::singleShot(500, this, [=]() {
            this->hide();   // 自己隐藏

            // 设置chooseScene场景的位置
            chooseScene->setGeometry(this->geometry());
            chooseScene->show();

        });
    });

    // 关卡选择场景切换到主场景
    connect(chooseScene, &ChooseLevelScene::showMainnSceneSignal, [=]() {
        this->setGeometry(chooseScene->geometry());

        this->show();
    });


}

MainScene::~MainScene()
{
    delete ui;
}

void MainScene::paintEvent(QPaintEvent *event)
{
    // 添加背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");

    painter.drawPixmap(0, 0, width(), height(), pix);


    // 上方的背景添加
    pix.load(":/res/Title.png");
    pix = pix.scaled(0.7*pix.width(), 0.7*pix.height());
    painter.drawPixmap(10, 30, pix);


}
